import {Component} from '@angular/core';
import {LoginService} from "./login.service";
import {ms_loginUser} from "./model/ms_loginUser";
import {Router} from "@angular/router";
import {CheckUserService} from "./check-user.service";
import {UserAuthService} from "./manage/user-auth.service";
import {RegisterService} from "./register.service";
import {ms_registerUser} from "./model/ms_registerUser";
import {BaseComponent} from "./base.component";


/**
 * 登录、注册
 */
@Component({
    selector: 'my-app',
    templateUrl:"./login.component.html",
    styleUrls:["./login.component.css"],
    providers:[
        LoginService,
        RegisterService
    ]
})
export class LoginComponent  extends BaseComponent {




    /**
     * @type {ms_loginUser} 登录对象
     */
    protected user:ms_loginUser =new ms_loginUser();


    /**
     *
     * @type {ms_registerUser} 注册对象
     */
    register:ms_registerUser=new ms_registerUser();


    /**
     * true:登录
     *
     * false:注册
     *
     * @type {boolean}
     */
    public switchLoginAndSignin:number=0;



    constructor(private loginService:LoginService,private router:Router,
                private check:CheckUserService,private userAuth:UserAuthService,
                private siginServer:RegisterService)
    {super()}


    public onLogin(){

        this.isAjax=true;

        this._message="";

        let request=this.loginService.Login(this.user);
        request.then(request=>{
            this.isAjax=false;
            if(request.result)
            {
                let str=this.check.RouteUrl||"";
                this.check.userToken.token=request.data.token;
                str=(str==""?"manage":str);
                this.userAuth.isCheck=true;
                this.router.navigate([str]);
            }else
            {
                this._message=request.message;
            }
        }).catch(ex=>{
            super.setMessage(ex,"登录超时");
            this.isAjax=false;
        });

    }

    public onSignin(){

        this.isAjax=true;
        this._message="";

        let request=this.siginServer.SignIn(this.register);
        request.then(request=>{
            this.isAjax=false;
            if(request.result)
            {
                let str=this.check.RouteUrl||"";
                this.check.userToken.token=request.data.token;
                str=(str==""?"manage":str);
                this.userAuth.isCheck=true;
                this.router.navigate([str]);
            }else
            {
                this._message=request.message;
            }
        }).catch(ex=>{
            this.isAjax=false;
            super.setMessage(ex,"网络错误");
        });

    }

    public switch(val:number):number{
        this.switchLoginAndSignin=val;
        this._message="";
        switch (val)
        {
            case 0:
                this.validatorUser();
                break;

            case 1:
                this.validatorRegister();
                break;

            default:
                break;
        }


        /*if(val)
        {
           this.user.userName="";
           this.user.password="";
        }else
        {
            this.register.userName="";
            this.register.password="";
            this.register.invalidPassword="";

        }*/

        return this.switchLoginAndSignin;
    }


    /**
     * 验证用户登录时用户名密码是否符合规则
     *
     * @returns {boolean}
     */
    validatorUser():boolean{

        let results=this.user.getValidator();
        this._message=results.message;

        return results.result;
    }


    /**
     * 验证注册时填写信息是否符合规则
     * @returns {boolean}
     */
    validatorRegister():boolean{

        let results=this.register.getValidator();
        this._message=results.message;
        return results.result;

    }


}
